/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id: ProjectTeamXMLReader.java 1804125 2017-08-04 14:15:05Z ssteiner $ */

package cn.buk.tms.dto;

//Java

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

//SAX


/**
 * XMLReader implementation for the ProjectTeam class. This class is used to
 * generate SAX events from the ProjectTeam class.
 */
public class ItineraryXMLReader extends AbstractObjectReader {

    /**
     * @see org.xml.sax.XMLReader#parse(InputSource)
     */
    @Override
    public void parse(InputSource input) throws SAXException {
        if (input instanceof ItineraryInputSource) {
            parse(((ItineraryInputSource)input).getItineraryInfo());
        } else {
            throw new SAXException("Unsupported InputSource specified. "
                    + "Must be a ProjectTeamInputSource");
        }
    }


    /**
     * Starts parsing the ProjectTeam object.
     * @param itineraryInfo The object to parse
     * @throws SAXException In case of a problem during SAX event generation
     */
    public void parse(ItineraryInfo itineraryInfo) throws SAXException {
        if (itineraryInfo == null) {
            throw new NullPointerException("Parameter projectTeam must not be null");
        }
        if (handler == null) {
            throw new IllegalStateException("ContentHandler not set");
        }

        //Start the document
        handler.startDocument();

        //Generate SAX events for the ProjectTeam
        generateFor(itineraryInfo);

        //End the document
        handler.endDocument();
    }


    /**
     * Generates SAX events for a ProjectTeam object.
     * @param itineraryInfo ProjectTeam object to use
     * @throws SAXException In case of a problem during SAX event generation
     */
    protected void generateFor(ItineraryInfo itineraryInfo) throws SAXException {
        if (itineraryInfo == null) {
            throw new NullPointerException("Parameter projectTeam must not be null");
        }
        if (handler == null) {
            throw new IllegalStateException("ContentHandler not set");
        }

        handler.startElement("itinerary-info");
        handler.element("projectName", itineraryInfo.getProjectName());
        handler.element("companySign", itineraryInfo.getCompanySign());
        for (Object o : itineraryInfo.getFlights()) {
            ItineraryFlightInfo member = (ItineraryFlightInfo) o;
            generateFor(member);
        }

        for (ItineraryPassengerInfo o : itineraryInfo.getPassengers()) {
            generateFor(o);
        }
        handler.endElement("itinerary-info");
    }

    /**
     * Generates SAX events for a ProjectMember object.
     * @param itineraryFlightInfo ProjectMember object to use
     * @throws SAXException In case of a problem during SAX event generation
     */
    protected void generateFor(ItineraryFlightInfo itineraryFlightInfo) throws SAXException {
        if (itineraryFlightInfo == null) {
            throw new NullPointerException("Parameter itineraryFlightInfo must not be null");
        }
        if (handler == null) {
            throw new IllegalStateException("ContentHandler not set");
        }

        handler.startElement("flights");
        handler.element("dport", itineraryFlightInfo.getDport());
        handler.element("dportNameCn", itineraryFlightInfo.getDportNameCn());
        handler.element("dportNameEn", itineraryFlightInfo.getDportNameEn());

        handler.element("aport", itineraryFlightInfo.getAport());
        handler.element("aportNameCn", itineraryFlightInfo.getAportNameCn());
        handler.element("aportNameEn", itineraryFlightInfo.getAportNameEn());

        handler.element("flightNo", itineraryFlightInfo.getFlightNo());
        handler.element("subclass", itineraryFlightInfo.getSubclass());

        handler.element("ddate", itineraryFlightInfo.getDdate());
        handler.element("dtime", itineraryFlightInfo.getDtime());
        handler.element("atime", itineraryFlightInfo.getAtime());
        handler.element("dterm", itineraryFlightInfo.getDterm());
        handler.element("aterm", itineraryFlightInfo.getAterm());

        handler.endElement("flights");
    }

    protected void generateFor(ItineraryPassengerInfo info) throws SAXException {
        if (info == null) {
            throw new NullPointerException("Parameter ItineraryPassengerInfo must not be null");
        }
        if (handler == null) {
            throw new IllegalStateException("ContentHandler not set");
        }

        handler.startElement("passengers");
        handler.element("name", info.getName());
        handler.element("idType", info.getIdType() + "");
        handler.element("idTypeDesc", info.getIdTypeDesc());
        handler.element("idNo", info.getIdNo());

        handler.element("ticketNo", info.getTicketNo());

        handler.endElement("passengers");
    }

}
